package com.pty.demo.structural.flyweight;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @author pengty
 * @date 2020/3/12
 */
public class RifleFactory {

    private static Map<String, Rifle> rifleMap = Maps.newHashMap();

    public static Rifle getRifle(Integer type, String name) {
        Rifle rifle = rifleMap.get(name);
        if (null == rifle) {
            if (type == 1) {
                rifle = new ShootingRifle(name);
                rifleMap.put(name, rifle);
            }
            if (type == 2) {
                rifle = new SniperRifle(name);
                rifleMap.put(name, rifle);
            }
            System.out.println("初始化创建" + name + "步枪");
        }
        System.out.println("获得" + name + "步枪");
        return rifle;
    }
}
